package com.gxa.dr2.controller;

import com.gxa.dr2.dto.LayDTO;
import com.gxa.dr2.dto.ResultDTO;
import com.gxa.dr2.param.MyParam;
import com.gxa.dr2.pojo.Empl;
import com.gxa.dr2.service.EmplService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("empl")
public class EmplController {

    @Autowired
    private EmplService emplService;

    /**
     * 员工列表页面
     * @return
     */
    @GetMapping("list/page")
    public String listPage(){
        return "empl/empl-list";
    }


    /**
     * 列表数据
     * @return
     */
    @PostMapping("list/data")
    @ResponseBody
    public LayDTO list(MyParam param){
        return emplService.list(param);
    }


    /**
     * 员工添加页面
     * @return
     */
    @GetMapping("add/page")
    public String addPage(){
        return "empl/empl-add";
    }


    /**
     * 员工的添加接口
     * @return
     */
    @PostMapping("add/do")
    @ResponseBody
    public ResultDTO addDo(Empl empl, @RequestParam(name = "roleIds[]") Integer[] roleIds){
        return emplService.add(empl, roleIds);
    }


    /**
     * 修改状态
     * @return
     */
    @PostMapping("change/status")
    @ResponseBody
    public ResultDTO changeStatus(Empl empl){
        return emplService.changeStatus(empl);
    }

    @PostMapping("change/phone/{phone}")
    @ResponseBody
    public ResultDTO changePhone(@PathVariable Long phone, Empl empl, HttpSession session) {
        return emplService.changePhone(empl, phone, session);
    }

    @PostMapping("change/emplCode/{emplCode}")
    @ResponseBody
    public ResultDTO changeEmplCode(@PathVariable String emplCode, Empl empl, HttpSession session) {
        return emplService.changeEmplCode(empl, emplCode, session);
    }

    @PostMapping("change/pwd")
    @ResponseBody
    public ResultDTO changePWD(@RequestParam(name="password") String pwd,
                               @RequestParam(name="new-password") String newPWD,
                               @RequestParam(name="new-password2") String newPWD2,
                               HttpSession session){

        return emplService.changePWD(session, pwd, newPWD, newPWD2);
    }

    /**
     * 员工编辑页面
     * @return
     */
    @GetMapping("edit/page")
    public String editPage(){
        return "empl/empl-edit";
    }

    /**
     * 员工的添加接口
     * @return
     */
    @PostMapping("edit/do")
    @ResponseBody
    public ResultDTO editDo(Empl empl, @RequestParam(name = "roleIds[]") Integer[] roleIds){
        return emplService.edit(empl, roleIds);
    }


    /**
     * 员工的详细信息
     * @return
     */
    @PostMapping("detail/data")
    @ResponseBody
    public ResultDTO detail(Integer emplId){
        return emplService.detail(emplId);
    }


    /**
     * 删除
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public ResultDTO delete(Integer emplId){
        return emplService.deleteFalse(emplId);
    }


    /**
     * 批量删除员工的数据
     * @return
     */
    @PostMapping("deleteAll")
    @ResponseBody
    public ResultDTO deletAll(@RequestParam(name = "ids[]") Integer[] ids){
        return emplService.deleteAll(ids);
    }


}
